This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
library(tidyverse)
library(readr)
# directory with CSV files
root_dir <- "D:\\Cloud Drives\\OneDrive - purdue.edu\\Second Ford\\Year 3\\driving_data\\result\\all\\"
# list all CSV files
all_csv_files <- list.files(path = root_dir, pattern = "\\.csv$", full.names = TRUE, recursive = TRUE)
# read all CSV files into a list of dataframes
list_df <- lapply(all_csv_files, function(x) {
df <- read_csv(x)
df$A <- basename(x) # file name
# df$B <- dirname(x) # directory name
df
})
# bind all dataframes into one
df <- bind_rows(list_df)
df.label <- df %>%
mutate(
#extract the task name from file name
B = str_sub(A, start = 10, end = 14),
# extract task letter from file name
task = case_when(
str_starts(B, "Pu") ~ "P",
str_starts(B, "Po") ~ "M",
str_starts(B, "D") ~ "D",
str_starts(B, "J") ~ "J",
str_starts(B, "W") ~ "W",
TRUE ~ B),
#extract the digits for date and time
date = str_extract(A, pattern = "(\\d{14})"),
#extract participant number, remove the letter P
id = str_replace(subject, pattern = "^P", replacement = ""),
#correct column type
task = as.factor(task),
id = as.numeric(id),
date = as.numeric(date)
)
str(df.label)
tibble [3,454,664 × 27] (S3: tbl_df/tbl/data.frame)
$ Frame : num [1:3454664] 26286 26287 26288 26289 26290 ...
$ veh_speed : num [1:3454664] 0.00355 0.00311 0.00269 0.00223 0.00176 ...
$ stream_1 : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ Acc_Position : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ Brake_Force : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ Steering_Angle: num [1:3454664] 4.5 4.5 4.5 4.5 4.5 ...
$ Steering_Rate : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ CVED.ID : num [1:3454664] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
$ Distance : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ b2b_time : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ b2b_distance : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ time_to_col : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ velocity : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ x_coord : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ y_coord : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ z_coord : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ type : num [1:3454664] 0 0 1 1 1 1 1 1 1 1 ...
$ offset : num [1:3454664] 0 0 0.0189 0.0189 0.0189 ...
$ width : num [1:3454664] 0 0 12 12 12 12 12 12 12 12 ...
$ lane_id : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ AD_State : num [1:3454664] 1 1 0 0 0 0 0 0 0 0 ...
$ subject : chr [1:3454664] "P2" "P2" "P2" "P2" ...
$ A : chr [1:3454664] "FORD2023-Drive235_20230515112723.csv" "FORD2023-Drive235_20230515112723.csv" "FORD2023-Drive235_20230515112723.csv" "FORD2023-Drive235_20230515112723.csv" ...
$ B : chr [1:3454664] "Drive" "Drive" "Drive" "Drive" ...
$ task : Factor w/ 5 levels "D","J","M","P",..: 1 1 1 1 1 1 1 1 1 1 ...
$ date : num [1:3454664] 2.02e+13 2.02e+13 2.02e+13 2.02e+13 2.02e+13 ...
$ id : num [1:3454664] 2 2 2 2 2 2 2 2 2 2 ...
# extract transition key moments: start, end TOR change, AD state change
key_moment_raw <- df.label %>%
arrange(id, task) %>%
group_by(id, task) %>%
mutate(
log_change = stream_1 != lag(stream_1, default = stream_1[1]),
AD_change = AD_State != lag(AD_State, default = AD_State[1]),
start = Frame == min(Frame),
end = Frame == max(Frame)
) %>%
filter(log_change | AD_change | start | end) %>%
select(id, task, Frame, stream_1, AD_State)
key_moment_clean <- df.label %>%
arrange(id, task) %>%
group_by(id, task) %>%
mutate(
log_change = stream_1 != lag(stream_1, default = stream_1[1]),
AD_change = AD_State != lag(AD_State, default = AD_State[1]),
start = Frame == min(Frame),
end = Frame == max(Frame)
) %>%
filter(log_change | AD_change | start | end) %>% filter(
((stream_1 == 7 & AD_State == 0 & lead(stream_1) == 8 & lead(AD_State) == 0 & lead(stream_1, 2) == 8 & lead(AD_State, 2) == 1) |
(lag(stream_1) == 7 & lag(AD_State) == 0 & stream_1 == 8 & AD_State == 0 & lead(stream_1) == 8 & lead(AD_State) == 1) |
(lag(stream_1, 2) == 7 & lag(AD_State, 2) == 0 & lag(stream_1) == 8 & lag(AD_State) == 0 & stream_1 == 8 & AD_State == 1)) | start |end | lag(stream_1, default = stream_1[1]) == 0 & stream_1 == 7) %>%
group_by(id, task) %>%
mutate(label = row_number()) %>%
select(id, task, Frame, label) %>% pivot_wider(names_from = label, values_from = Frame)
# Export the data frame to a CSV file
#write_csv(key_moment_raw, "keymoment_raw.csv")
#write_csv(df.label, "year3_driving_all.csv")
#write_csv(steer_angle, "steering.csv")
#write_csv(df.check, "check.csv")
#write_csv(acc_position, "gas.csv")
write_csv(driving.label, "videocap_label.csv")
df.check <- df.label %>% filter(id == 39, task == "J")
steer_angle = df.label %>%
group_by(id, task) %>%
filter(stream_1 >=7) %>%
#use the steerhing angle at TOR1 as reference
mutate(initial_angle = first(Steering_Angle)) %>%
filter(abs(Steering_Angle-initial_angle)>=2) %>%
summarise(T_steer = first(Frame))
`summarise()` has grouped output by 'id'. You can override using the `.groups` argument.
header <- steer_angle%>%
select(id, task)
acc_position = df.label %>%
group_by(id, task) %>%
filter(stream_1 >=7) %>%
#use the steerhing angle at TOR1 as reference
filter(Acc_Position >=0.03) %>%
summarise(T_gas = first(Frame)) %>%
full_join(header, by = c("id", "task")) %>%
arrange(id,task)
`summarise()` has grouped output by 'id'. You can override using the `.groups` argument.
#file names
driving.label <- df.label %>%
select(id,task,date) %>%
distinct(task, date, id, .keep_all = TRUE) %>%
mutate(filename = paste0(as.character(date), "VCAP1.mp4")) %>%
arrange(id,task) %>%
rename(`timestamp` = date) %>%
mutate(
date = stringr::str_sub(as.character(timestamp), 5, 8), # Extract MMDD
time = stringr::str_c(
stringr::str_sub(as.character(timestamp), 9, 10), ":", # Extract HH
stringr::str_sub(as.character(timestamp), 11, 12), ":", # Extract MM
stringr::str_sub(as.character(timestamp), 13, 14) # Extract SS
)
)
str(driving.label)
tibble [230 × 6] (S3: tbl_df/tbl/data.frame)
$ id : num [1:230] 2 2 2 2 2 3 3 3 3 3 ...
$ task : Factor w/ 5 levels "D","J","M","P",..: 1 2 3 4 5 1 2 3 4 5 ...
$ timestamp: num [1:230] 2.02e+13 2.02e+13 2.02e+13 2.02e+13 2.02e+13 ...
$ filename : chr [1:230] "20230515112723VCAP1.mp4" "20230515114301VCAP1.mp4" "20230515112025VCAP1.mp4" "20230515113307VCAP1.mp4" ...
$ date : chr [1:230] "0515" "0515" "0515" "0515" ...
$ time : chr [1:230] "11:27:23" "11:43:01" "11:20:25" "11:33:07" ...
driving.df = read_csv(".\\year3_driving_all.csv") %>% select(id, task, Frame, veh_speed, time_to_col, b2b_distance, offset, lane_id)
Rows: 3454664 Columns: 27── Column specification ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (5): subject, A, B, task, id
dbl (22): Frame, veh_speed, stream_1, Acc_Position, Brake_Force, Steering_Angle, Steering_Rate, CVED.ID, Distance, b2b_time, b2b_distance, time_to_col, velocity, x_coord, y_coor...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(driving.df)
tibble [3,454,664 × 8] (S3: tbl_df/tbl/data.frame)
$ id : chr [1:3454664] "2" "2" "2" "2" ...
$ task : chr [1:3454664] "D" "D" "D" "D" ...
$ Frame : num [1:3454664] 26286 26287 26288 26289 26290 ...
$ veh_speed : num [1:3454664] 0.00355 0.00311 0.00269 0.00223 0.00176 ...
$ time_to_col : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ b2b_distance: num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
$ offset : num [1:3454664] 0 0 0.0189 0.0189 0.0189 ...
$ lane_id : num [1:3454664] 0 0 0 0 0 0 0 0 0 0 ...
Now read in the RT file for key moments
key_moments = read_csv(".\\RT.csv") %>% select(id, task, TOR1)
New names:Rows: 230 Columns: 17── Column specification ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (2): task, RT_gas
dbl (13): id, START, TOR1, AD_off, TOR2, AD_On, END, abnormal, RT_adoff, T_steer, RT_steer, T_gas, window start_fromend
lgl (1): ...15
time (1): ...17
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(key_moments)
tibble [230 × 3] (S3: tbl_df/tbl/data.frame)
$ id : num [1:230] 2 2 2 2 2 3 3 3 3 3 ...
$ task: chr [1:230] "D" "J" "M" "P" ...
$ TOR1: num [1:230] 40509 92460 16572 58718 269142 ...
library(signal)
interpolate_na <- function(x) {
if (all(is.na(x))) {
return(x) # return as is if all values are NA
}
na.approx(x, rule = 2)
}
lane_position = driving.df %>% select(id, task, Frame, offset, lane_id) %>% mutate(id = as.numeric(id)) %>%
#adjust lane position to one coordinate, regardless of lane number
mutate(offset_adj = case_when(lane_id == 0 ~ 6-offset,
lane_id == 1 ~ (6-offset)+12,
lane_id == 2 ~ (6-offset)+24)) %>%
group_by(id, task) %>%
#interpolate NA values
mutate(across(4, interpolate_na)) %>%
#apply Savitzky-Golay filter to smooth out data (window 0.5s)
mutate(offset_adj_filt = sgolayfilt(offset_adj, p = 3, n = 31)) %>% # p is the polynomial order3, window length 3 seconds (90 frames),
#include key moment TOR1 for data truncation TOR1 - TOR1+10
left_join(key_moments, by = c("id", "task")) %>%
mutate(frame_adj= Frame-TOR1) %>%
filter(frame_adj>=0 & frame_adj<=600)
str(lane_position)
gropd_df [138,230 × 9] (S3: grouped_df/tbl_df/tbl/data.frame)
$ id : num [1:138230] 2 2 2 2 2 2 2 2 2 2 ...
$ task : chr [1:138230] "D" "D" "D" "D" ...
$ Frame : num [1:138230] 40509 40510 40511 40512 40513 ...
$ offset : num [1:138230] -0.197 -0.197 -0.197 -0.197 -0.197 ...
$ lane_id : num [1:138230] 0 0 0 0 0 0 0 0 0 0 ...
$ offset_adj : num [1:138230] 6.2 6.2 6.2 6.2 6.2 ...
$ offset_adj_filt: num [1:138230] 6.2 6.2 6.2 6.2 6.2 ...
$ TOR1 : num [1:138230] 40509 40509 40509 40509 40509 ...
$ frame_adj : num [1:138230] 0 1 2 3 4 5 6 7 8 9 ...
- attr(*, "groups")= tibble [230 × 3] (S3: tbl_df/tbl/data.frame)
..$ id : num [1:230] 2 2 2 2 2 3 3 3 3 3 ...
..$ task : chr [1:230] "D" "J" "M" "P" ...
..$ .rows: list<int> [1:230]
.. ..$ : int [1:601] 1 2 3 4 5 6 7 8 9 10 ...
.. ..$ : int [1:601] 27647 27648 27649 27650 27651 27652 27653 27654 27655 27656 ...
.. ..$ : int [1:601] 55293 55294 55295 55296 55297 55298 55299 55300 55301 55302 ...
.. ..$ : int [1:601] 82939 82940 82941 82942 82943 82944 82945 82946 82947 82948 ...
.. ..$ : int [1:601] 110585 110586 110587 110588 110589 110590 110591 110592 110593 110594 ...
.. ..$ : int [1:601] 602 603 604 605 606 607 608 609 610 611 ...
.. ..$ : int [1:601] 28248 28249 28250 28251 28252 28253 28254 28255 28256 28257 ...
.. ..$ : int [1:601] 55894 55895 55896 55897 55898 55899 55900 55901 55902 55903 ...
.. ..$ : int [1:601] 83540 83541 83542 83543 83544 83545 83546 83547 83548 83549 ...
.. ..$ : int [1:601] 111186 111187 111188 111189 111190 111191 111192 111193 111194 111195 ...
.. ..$ : int [1:601] 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 ...
.. ..$ : int [1:601] 30652 30653 30654 30655 30656 30657 30658 30659 30660 30661 ...
.. ..$ : int [1:601] 58298 58299 58300 58301 58302 58303 58304 58305 58306 58307 ...
.. ..$ : int [1:601] 85944 85945 85946 85947 85948 85949 85950 85951 85952 85953 ...
.. ..$ : int [1:601] 113590 113591 113592 113593 113594 113595 113596 113597 113598 113599 ...
.. ..$ : int [1:601] 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 ...
.. ..$ : int [1:601] 29450 29451 29452 29453 29454 29455 29456 29457 29458 29459 ...
.. ..$ : int [1:601] 57096 57097 57098 57099 57100 57101 57102 57103 57104 57105 ...
.. ..$ : int [1:601] 84742 84743 84744 84745 84746 84747 84748 84749 84750 84751 ...
.. ..$ : int [1:601] 112388 112389 112390 112391 112392 112393 112394 112395 112396 112397 ...
.. ..$ : int [1:601] 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 ...
.. ..$ : int [1:601] 31854 31855 31856 31857 31858 31859 31860 31861 31862 31863 ...
.. ..$ : int [1:601] 59500 59501 59502 59503 59504 59505 59506 59507 59508 59509 ...
.. ..$ : int [1:601] 87146 87147 87148 87149 87150 87151 87152 87153 87154 87155 ...
.. ..$ : int [1:601] 114792 114793 114794 114795 114796 114797 114798 114799 114800 114801 ...
.. ..$ : int [1:601] 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 ...
.. ..$ : int [1:601] 30051 30052 30053 30054 30055 30056 30057 30058 30059 30060 ...
.. ..$ : int [1:601] 57697 57698 57699 57700 57701 57702 57703 57704 57705 57706 ...
.. ..$ : int [1:601] 85343 85344 85345 85346 85347 85348 85349 85350 85351 85352 ...
.. ..$ : int [1:601] 112989 112990 112991 112992 112993 112994 112995 112996 112997 112998 ...
.. ..$ : int [1:601] 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 ...
.. ..$ : int [1:601] 28849 28850 28851 28852 28853 28854 28855 28856 28857 28858 ...
.. ..$ : int [1:601] 56495 56496 56497 56498 56499 56500 56501 56502 56503 56504 ...
.. ..$ : int [1:601] 84141 84142 84143 84144 84145 84146 84147 84148 84149 84150 ...
.. ..$ : int [1:601] 111787 111788 111789 111790 111791 111792 111793 111794 111795 111796 ...
.. ..$ : int [1:601] 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 ...
.. ..$ : int [1:601] 32455 32456 32457 32458 32459 32460 32461 32462 32463 32464 ...
.. ..$ : int [1:601] 60101 60102 60103 60104 60105 60106 60107 60108 60109 60110 ...
.. ..$ : int [1:601] 87747 87748 87749 87750 87751 87752 87753 87754 87755 87756 ...
.. ..$ : int [1:601] 115393 115394 115395 115396 115397 115398 115399 115400 115401 115402 ...
.. ..$ : int [1:601] 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 ...
.. ..$ : int [1:601] 31253 31254 31255 31256 31257 31258 31259 31260 31261 31262 ...
.. ..$ : int [1:601] 58899 58900 58901 58902 58903 58904 58905 58906 58907 58908 ...
.. ..$ : int [1:601] 86545 86546 86547 86548 86549 86550 86551 86552 86553 86554 ...
.. ..$ : int [1:601] 114191 114192 114193 114194 114195 114196 114197 114198 114199 114200 ...
.. ..$ : int [1:601] 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 ...
.. ..$ : int [1:601] 33056 33057 33058 33059 33060 33061 33062 33063 33064 33065 ...
.. ..$ : int [1:601] 60702 60703 60704 60705 60706 60707 60708 60709 60710 60711 ...
.. ..$ : int [1:601] 88348 88349 88350 88351 88352 88353 88354 88355 88356 88357 ...
.. ..$ : int [1:601] 115994 115995 115996 115997 115998 115999 116000 116001 116002 116003 ...
.. ..$ : int [1:601] 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 ...
.. ..$ : int [1:601] 36662 36663 36664 36665 36666 36667 36668 36669 36670 36671 ...
.. ..$ : int [1:601] 64308 64309 64310 64311 64312 64313 64314 64315 64316 64317 ...
.. ..$ : int [1:601] 91954 91955 91956 91957 91958 91959 91960 91961 91962 91963 ...
.. ..$ : int [1:601] 119600 119601 119602 119603 119604 119605 119606 119607 119608 119609 ...
.. ..$ : int [1:601] 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 ...
.. ..$ : int [1:601] 33657 33658 33659 33660 33661 33662 33663 33664 33665 33666 ...
.. ..$ : int [1:601] 61303 61304 61305 61306 61307 61308 61309 61310 61311 61312 ...
.. ..$ : int [1:601] 88949 88950 88951 88952 88953 88954 88955 88956 88957 88958 ...
.. ..$ : int [1:601] 116595 116596 116597 116598 116599 116600 116601 116602 116603 116604 ...
.. ..$ : int [1:601] 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 ...
.. ..$ : int [1:601] 34258 34259 34260 34261 34262 34263 34264 34265 34266 34267 ...
.. ..$ : int [1:601] 61904 61905 61906 61907 61908 61909 61910 61911 61912 61913 ...
.. ..$ : int [1:601] 89550 89551 89552 89553 89554 89555 89556 89557 89558 89559 ...
.. ..$ : int [1:601] 117196 117197 117198 117199 117200 117201 117202 117203 117204 117205 ...
.. ..$ : int [1:601] 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 ...
.. ..$ : int [1:601] 34859 34860 34861 34862 34863 34864 34865 34866 34867 34868 ...
.. ..$ : int [1:601] 62505 62506 62507 62508 62509 62510 62511 62512 62513 62514 ...
.. ..$ : int [1:601] 90151 90152 90153 90154 90155 90156 90157 90158 90159 90160 ...
.. ..$ : int [1:601] 117797 117798 117799 117800 117801 117802 117803 117804 117805 117806 ...
.. ..$ : int [1:601] 15627 15628 15629 15630 15631 15632 15633 15634 15635 15636 ...
.. ..$ : int [1:601] 43273 43274 43275 43276 43277 43278 43279 43280 43281 43282 ...
.. ..$ : int [1:601] 70919 70920 70921 70922 70923 70924 70925 70926 70927 70928 ...
.. ..$ : int [1:601] 98565 98566 98567 98568 98569 98570 98571 98572 98573 98574 ...
.. ..$ : int [1:601] 126211 126212 126213 126214 126215 126216 126217 126218 126219 126220 ...
.. ..$ : int [1:601] 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 ...
.. ..$ : int [1:601] 35460 35461 35462 35463 35464 35465 35466 35467 35468 35469 ...
.. ..$ : int [1:601] 63106 63107 63108 63109 63110 63111 63112 63113 63114 63115 ...
.. ..$ : int [1:601] 90752 90753 90754 90755 90756 90757 90758 90759 90760 90761 ...
.. ..$ : int [1:601] 118398 118399 118400 118401 118402 118403 118404 118405 118406 118407 ...
.. ..$ : int [1:601] 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 ...
.. ..$ : int [1:601] 37864 37865 37866 37867 37868 37869 37870 37871 37872 37873 ...
.. ..$ : int [1:601] 65510 65511 65512 65513 65514 65515 65516 65517 65518 65519 ...
.. ..$ : int [1:601] 93156 93157 93158 93159 93160 93161 93162 93163 93164 93165 ...
.. ..$ : int [1:601] 120802 120803 120804 120805 120806 120807 120808 120809 120810 120811 ...
.. ..$ : int [1:601] 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 ...
.. ..$ : int [1:601] 38465 38466 38467 38468 38469 38470 38471 38472 38473 38474 ...
.. ..$ : int [1:601] 66111 66112 66113 66114 66115 66116 66117 66118 66119 66120 ...
.. ..$ : int [1:601] 93757 93758 93759 93760 93761 93762 93763 93764 93765 93766 ...
.. ..$ : int [1:601] 121403 121404 121405 121406 121407 121408 121409 121410 121411 121412 ...
.. ..$ : int [1:601] 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 ...
.. ..$ : int [1:601] 36061 36062 36063 36064 36065 36066 36067 36068 36069 36070 ...
.. ..$ : int [1:601] 63707 63708 63709 63710 63711 63712 63713 63714 63715 63716 ...
.. ..$ : int [1:601] 91353 91354 91355 91356 91357 91358 91359 91360 91361 91362 ...
.. ..$ : int [1:601] 118999 119000 119001 119002 119003 119004 119005 119006 119007 119008 ...
.. ..$ : int [1:601] 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 ...
.. ..$ : int [1:601] 37263 37264 37265 37266 37267 37268 37269 37270 37271 37272 ...
.. ..$ : int [1:601] 64909 64910 64911 64912 64913 64914 64915 64916 64917 64918 ...
.. ..$ : int [1:601] 92555 92556 92557 92558 92559 92560 92561 92562 92563 92564 ...
.. .. [list output truncated]
.. ..@ ptype: int(0)
..- attr(*, ".drop")= logi TRUE
#visualise filtering result
str(df_filter)
tibble [252,833 × 13] (S3: tbl_df/tbl/data.frame)
$ id : chr [1:252833] "10" "10" "10" "10" ...
$ task : chr [1:252833] "D" "D" "D" "D" ...
$ frame : num [1:252833] 1 2 3 4 5 6 7 8 9 10 ...
$ right_eye_x : num [1:252833] 280 280 280 280 280 ...
$ right_eye_y : num [1:252833] 72.8 72.9 72.9 73 72.9 ...
$ right_ear_x : num [1:252833] 238 238 238 237 238 ...
$ right_ear_y : num [1:252833] 87.1 87.4 87.5 87.5 87.5 ...
$ right_wrist_x : num [1:252833] 373 372 372 372 372 ...
$ right_wrist_y : num [1:252833] 303 302 302 302 302 ...
$ right_wrist_x_filt: num [1:252833] 372 372 372 372 372 ...
$ right_wrist_y_filt: num [1:252833] 303 303 302 302 302 ...
$ right_eye_x_filt : num [1:252833] 280 280 280 280 280 ...
$ right_eye_y_filt : num [1:252833] 72.8 72.9 72.9 72.9 73 ...
df_filtered = lane_position %>% dplyr::filter(task == "D") %>% dplyr::filter(id == 10)
ggplot(df_filtered, aes(x = frame_adj)) +
geom_line(aes(y = offset_adj), color = 'blue', alpha = 0.5) +
#geom_line(aes(y = offset_adj_filt), color = 'red') +
theme_minimal() +
labs(title = "Savitzky-Golay Filter", x = "frame", y = "lane_offset")
library(dplyr)
library(ggplot2)
library(plotly)
# Consolidating lines and computing confidence interval
consolidated_data <- lane_position %>%
group_by(task, frame_adj) %>%
dplyr::summarise(mean_offset = mean(offset_adj_filt),
lower_ci = mean(offset_adj_filt) - sd(offset_adj_filt)/sqrt(n()),
upper_ci = mean(offset_adj_filt) + sd(offset_adj_filt)/sqrt(n()))
`summarise()` has grouped output by 'task'. You can override using the `.groups` argument.
# Plotting consolidated lines with confidence intervals
consolidated_plot <- ggplot(consolidated_data, aes(x = frame_adj, y = mean_offset, color = task, fill = task)) +
geom_line() +
geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci), alpha = 0.3) +
labs(title = "Lane_Offset by Task", color = "Task") +
theme_minimal()
# Displaying the consolidated plot
ggplotly(consolidated_plot)
# creating individual plot
df = lane_position %>% dplyr::filter(task == "D")
# Calculate mean offset for each frame
mean_offsets <- consolidated_data %>% dplyr::filter(task == "D")
#extend color palette
library(RColorBrewer)
# Define the number of colors you want
nb.cols <- 46
mycolors <- colorRampPalette(brewer.pal(8, "Blues"))(nb.cols)
D<- ggplot(df, aes(x = frame_adj, y = offset_adj_filt, color = as.factor(id))) +
geom_line(aes(alpha = 0.5)) +
labs(title = "Driving Trajectory for Driving-Only Condition") +
geom_line(data = mean_offsets, aes(x = frame_adj, y = mean_offset), color = "red", size = 1) +
scale_fill_manual(values = mycolors) +
theme_minimal()
ggplotly(D)
# creating individual plot
df = lane_position %>% dplyr::filter(task == "P")
# Calculate mean offset for each frame
mean_offsets <- consolidated_data %>% dplyr::filter(task == "P")
P<- ggplot(df, aes(x = frame_adj, y = offset_adj_filt, color = as.factor(id))) +
geom_line(alpha = 0.5) +
labs(title = "Driving Trajectory for Puzzle Condition") +
geom_line(data = mean_offsets, aes(x = frame_adj, y = mean_offset), color = "red", size = 1) +
theme_minimal()
ggplotly(P)
# creating individual plot
df = lane_position %>% dplyr::filter(task == "J")
# Calculate mean offset for each frame
mean_offsets <- consolidated_data %>% dplyr::filter(task == "J")
J<- ggplot(df, aes(x = frame_adj, y = offset_adj_filt, color = as.factor(id))) +
geom_line(alpha = 0.5) +
labs(title = "Driving Trajectory for Jigsaw Condition") +
geom_line(data = mean_offsets, aes(x = frame_adj, y = mean_offset), color = "red", size = 1) +
theme_minimal()
ggplotly(J)
# creating individual plot
df = lane_position %>% dplyr::filter(task == "W")
# Calculate mean offset for each frame
mean_offsets <- consolidated_data %>% dplyr::filter(task == "W")
W<- ggplot(df, aes(x = frame_adj, y = offset_adj_filt, color = as.factor(id))) +
geom_line(alpha = 0.5) +
labs(title = "Driving Trajectory for Word Condition") +
geom_line(data = mean_offsets, aes(x = frame_adj, y = mean_offset), color = "red", size = 1) +
theme_minimal()
ggplotly(W)
# creating individual plot
df = lane_position %>% dplyr::filter(task == "M")
# Calculate mean offset for each frame
mean_offsets <- consolidated_data %>% dplyr::filter(task == "M")
M<- ggplot(df, aes(x = frame_adj, y = offset_adj_filt, color = as.factor(id))) +
geom_line(alpha = 0.5) +
labs(title = "Driving Trajectory for Poem Condition") +
geom_line(data = mean_offsets, aes(x = frame_adj, y = mean_offset), color = "red", size = 1) +
theme_minimal()
ggplotly(M)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.